package com.sinvo.oa.result;

import com.sinvo.oa.exception.CustomException;
import lombok.Data;

/**
 * @Description 通用返回结果类
 */
@Data
public class R<T> {
    //状态码
    private Integer code;
    //返回信息
    private String msg;
    //返回数据
    private T data;

    private static <T> R<T> build(T data) {
        R<T> r = new R<>();
        if (data != null)
            r.setData(data);
        return r;
    }

    private static <T> R<T> build(T data, ResultCode ResultCode) {
        R<T> r = build(data);
        r.setCode(ResultCode.getCode());
        r.setMsg(ResultCode.getMessage());
        return r;
    }

    private static <T> R<T> build(T data, Integer code, String message) {
        R<T> r = build(data);
        r.setCode(code);
        r.setMsg(message);
        return r;
    }

    public static <T> R<T> success(T data) {
        return build(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> success() {
        return R.success(null);
    }

    public static <T> R<T> fail(CustomException e) {
        return build(null, e.getCode(), e.getMessage());
    }

    public static <T> R<T> fail() {
        return R.build(null);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return R.build(null,resultCode);
    }

}
